package br.com.simora.relatorio;

import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import br.com.caelum.vraptor.Controller;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.view.Results;
import br.com.simora.controller.BasicController;
import br.com.simora.execucao.Execucao;
import br.com.simora.execucao.ExecucaoDAO;
import br.com.simora.musica.Musica;
import br.com.simora.radio.Radio;
import br.com.simora.radio.RadioDAO;

@Controller
public class RelatorioadminController extends BasicController {
	@Inject private ExecucaoDAO execucaoDAO;
	@Inject private RadioDAO radioDAO;
	
	public RelatorioadminController(Result result, ResourceBundle bundle,
			ExecucaoDAO execucaoDAO, RadioDAO radioDAO) {
    	this.result = result;
		this.bundle = bundle;
		this.execucaoDAO = execucaoDAO;
		this.radioDAO = radioDAO;
	}

    public RelatorioadminController(){}
    
	@PostConstruct
    public void init(){
        injetaUsuario();
    }
   
    @Path(value = "/" + RELATORIO + ADMIN + "/" + RADIO)
    @Get
    public void radio() {
        result.include("listaRadios", execucaoDAO.recuperaTodos(Radio.class));
    }
    
    @Path(value = "/" + RELATORIO + ADMIN + "/" + PERIODO)
    @Get
    public void periodo() {
        result.include("listaRadios", execucaoDAO.recuperaTodos(Radio.class));
    }
    
    @Path(value = "/" + RELATORIO + ADMIN + "/" + ESTADO)
    @Get
    public void estado() {
        result.include("listaEstados", estados);
    }
    
    @Path(value = "/" + RELATORIO + ADMIN + "/recuperaCidades")
    @Post
    public void recuperaCidades(String estadoSigla) {
    	result.use(Results.json()).withoutRoot().from(radioDAO.recuperaCidades(estadoSigla)).serialize();
    }
    
    @Path(value = "/" + RELATORIO + ADMIN + "/" + GERAR)
    @Post
    public void gerar(Radio radio, Musica musica, Date dataInicio, Date dataFim, String horaInicio, String horaFim) {
    	result.include("dataInicio", dataInicio);
    	result.include("dataFim", dataFim);
    	result.include("radio", radio);
    	long HOUR = 3600*1000;
    	dataInicio.setTime(dataInicio.getTime());
    	dataFim.setTime(dataFim.getTime()+((24*HOUR)-1));
    	System.out.println("data inicio " + dataInicio.toString());
    	System.out.println("data fim " + dataFim.toString());
    	if(radio != null && radio.getId() != null){
    		radio = (Radio) execucaoDAO.find(Radio.class, radio.getId());
    		List<Execucao> execs = execucaoDAO.recuperaExecucaoPorRadio(radio, dataInicio, dataFim);
    		result.include("listaExecucoes", execs);
    		result.redirectTo(RelatorioadminController.class).radio();
    	} else if(radio != null && radio.getEstado() != null && radio.getCidade() != null){
    		List<Execucao> execs = execucaoDAO.recuperaExecucaoPorFiltro(radio, musica, dataInicio, dataFim, null, null);
    		result.include("listaExecucoes", execs);
    		result.redirectTo(RelatorioadminController.class).estado();
    	}
    }
	
	@Override
	protected void redirect() {}

}